Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Adding a dynamic frame (C++)
Description: This tutorial teaches you how to add an extra dynamic frame to tf.Tutorial Level:
Next Tutorial: Tf and time (Python) (C++)
In the previous tutorial we added a fixed frame to the turtle. In this tutorial we'll add a dynamic frame to the turtle.
How to add a dynamic frame
Adding a dynamic frame is just a small step from adding a fixed frame. Go to the package we created for the previous tutorials:
$ roscd learning_tf
Fire up your favorite editor and paste the following code into a new file called src/dynamic_tf_broadcaster.cpp.
1 #include <ros/ros.h>
2 #include <tf/transform_broadcaster.h>
3
4 int main(int argc, char** argv){
5 ros::init(argc, argv, "my_tf_broadcaster");
6 ros::NodeHandle node;
7
8 ros::Rate rate(10.0);
9 double change = 0;
10 while (node.ok()){
11 tf::TransformBroadcaster br;
12 tf::Transform transform;
13 transform.setOrigin( tf::Vector3(2.0*sin(change), 2.0*cos(change), 0.0) );
14 transform.setRotation( tf::Quaternion(change, 0, 0) );
15 change += 0.1;
16
17 br.sendTransform(transform, ros::Time::now(), "carrot2", "turtle1");
18 rate.sleep();
19 }
20 return 0;
21 };
The code is very similar to the example in the adding a fixed frame tutorial. Only here the transform does change over time.
Let's take a look at the key line in this piece of code:
Here we create a new transform, from the parent "turtle1" to the new child "carrot2". The carrot2 frame is 1 meter offset from the turtle1 frame.
Running the dynamic frame broadcaster
Now that we created the code, lets compile it first. Open the CMakeLists.txt file, and add the following line on the bottom:
rosbuild_add_executable(dynamic_tf_broadcaster src/dynamic_tf_broadcaster.cpp)
and, try to build your package:
$ rosmake learning_tf
If everything went well, you should have a binary file in your bin folder. Check if you see a file called dynamic_tf_broadcaster:
$ ls bin/
If so, we're ready to edit the start_demo.launch launch file. Simply add the following line:
<launch> ... <node pkg="learning_tf" type="dynamic_tf_broadcaster" name="broadcaster_dynamic" /> </launch>
Now you're ready to start the turtle broadcaster demo:
$ roslaunch learning_tf start_demo.launch
You should see the turtle sim with two turtles.
Checking the results
Pretty much like in the last tutorial, we first need to change the listener to use the new dynamic frame we just added. Open the src/turtle_tf_listener.cpp file, and simple replace "/carrot1" with "/carrot2" in lines 26-27:
And now the good part: just restart the turtle demo, and you'll see the second turtle following the carrot instead of the first turtle! And even if you're not moving the first turtle around, the carrot should still move!
$ roslaunch learning_tf start_demo.launch
You're now ready to move to the next tutorial about tf and time (Python) (C++)